#!/bin/bash
# date: 2024/06/12
# auth：fearless11

# https://prometheus.io/docs
# https://prometheus.wang/quickstart/

# --web.enable-lifecycle
# curl -XPOST http://127.0.0.1:9090/-/reload

# promtool check config prometheus.yml
# promtool check rules rules.yml

mkdir /opt/soft /opt/app

download_prometheus() {
   # https://prometheus.io/download/
   wget https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz
   ls prometheus-2.52.0.linux-amd64.tar.gz | xargs -I{} tar xvf {} -C /opt/app

   unlink /usr/local/bin/prometheus
   unlink /usr/local/bin/promtool
   ln -s /opt/app/prometheus-2.52.0.linux-amd64/prometheus /usr/local/bin/prometheus
   ln -s /opt/app/prometheus-2.52.0.linux-amd64/promtool /usr/local/bin/promtool
   ln -s /opt/app/prometheus-2.52.0.linux-amd64 /opt/app/prometheus

   /bin/cp -fv prometheus.yml /opt/app/prometheus-2.52.0.linux-amd64/prometheus.yml
   /bin/cp -fv prometheus1.yml /opt/app/prometheus-2.52.0.linux-amd64/prometheus1.yml
   /bin/cp -fv system/prometheus.service /usr/lib/systemd/system/prometheus.service
}

download_alertmanager() {
   wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
   ls alertmanager-0.27.0.linux-amd64.tar.gz | xargs -I{} tar xvf {} -C /opt/app

   unlink /usr/local/bin/alertmanager
   unlink /usr/local/bin/amtool
   ln -s /opt/app/alertmanager-0.27.0.linux-amd64/alertmanager /usr/local/bin/alertmanager
   ln -s /opt/app/alertmanager-*/amtool /usr/local/bin/amtool
   ln -s /opt/app/alertmanager-0.27.0.linux-amd64 /opt/app/alertmanager

   /bin/cp -fv alertmanager.yml /opt/app/alertmanager-0.27.0.linux-amd64/alertmanager.yml
   /bin/cp -fv system/alertmanager.service /usr/lib/systemd/system/alertmanager.service

   # wehook
   curl -O https://raw.githubusercontent.com/prometheus/alertmanager/main/examples/webhook/echo.go
   go build -o /usr/local/bin/webhook echo.go
}

download_grafana() {
   # https://grafana.com/grafana/download
   sudo yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-11.0.0-1.x86_64.rpm
}

download_pushgateway() {
   wget https://github.com/prometheus/pushgateway/releases/download/v1.9.0/pushgateway-1.9.0.linux-amd64.tar.gz
}

download_thanos() {
   wget https://github.com/thanos-io/thanos/releases/download/v0.35.1/thanos-0.35.1.linux-amd64.tar.gz
   ls thanos-0.35.1.linux-amd64.tar.gz | xargs -I{} tar xvf {} -C /opt/app

   unlink /usr/local/bin/thanos
   ln -s /opt/app/thanos-0.35.1.linux-amd64/thanos /usr/local/bin/thanos
   ln -s /opt/app/thanos-0.35.1.linux-amd64 /opt/app/thanos

   /bin/cp -fv thanos_sidecar.yml /opt/app/thanos-0.35.1.linux-amd64/thanos_sidecar.yml
   /bin/cp -fv system/thanossidecar.service /usr/lib/systemd/system/thanossidecar.service
   /bin/cp -fv system/thanosstore.service /usr/lib/systemd/system/thanosstore.service
   /bin/cp -fv system/thanosquery.service /usr/lib/systemd/system/thanosquery.service
}

download_goreman() {
   wget https://github.com/mattn/goreman/releases/download/v0.3.9/goreman_v0.3.9_linux_amd64.tar.gz
   ls goreman_v0.3.9_linux_amd64.tar.gz | xargs -I{} tar xvf {} -C /opt/app
   unlink /usr/local/bin/goreman
   ln -s /opt/app/goreman_v0.3.9_linux_amd64/goreman /usr/local/bin/goreman
}

download_exporter() {
   wget https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
   wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz
   wget https://github.com/oliver006/redis_exporter/releases/download/v1.62.0/redis_exporter-v1.62.0.linux-amd64.tar.gz
   wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz
   wget https://github.com/danielqsj/kafka_exporter/releases/download/v1.7.0/kafka_exporter-1.7.0.linux-amd64.tar.gz
   wget https://github.com/kbudde/rabbitmq_exporter/releases/download/v1.0.0/rabbitmq_exporter_1.0.0_linux_amd64.tar.gz
   wget https://github.com/percona/mongodb_exporter/releases/download/v0.40.0/mongodb_exporter-0.40.0.linux-amd64.tar.gz

   ls node_exporter-1.8.1.linux-amd64.tar.gz \
      blackbox_exporter-0.25.0.linux-amd64.tar.gz \
      redis_exporter-v1.62.0.linux-amd64.tar.gz \
      mysqld_exporter-0.15.1.linux-amd64.tar.gz \
      kafka_exporter-1.7.0.linux-amd64.tar.gz \
      mongodb_exporter-0.40.0.linux-amd64.tar.gz | xargs -I{} tar xvf {} -C /opt/app
   mkdir /opt/app/rabbitmq_exporter-1.0.0/
   tar xf rabbitmq_exporter_1.0.0_linux_amd64.tar.gz -C /opt/app/rabbitmq_exporter-1.0.0/

   for i in $(echo node_exporter blackbox_exporter redis_exporter mysqld_exporter kafka_exporter rabbitmq_exporter mongodb_exporter); do
      unlink /usr/local/bin/$i
   done
   for i in $(echo node_exporter blackbox_exporter redis_exporter mysqld_exporter kafka_exporter rabbitmq_exporter mongodb_exporter); do
      ln -s /opt/app/$i-*/$i /usr/local/bin/$i
   done

   /bin/cp -fv blackbox.yml /opt/app/blackbox_exporter-0.25.0.linux-amd64/blackbox.yml
   /bin/cp -fv mysqld_exporter_my.cnf /opt/app/mysqld_exporter-0.15.1.linux-amd64/my.cnf
   # https://github.com/kbudde/rabbitmq_exporter/blob/main/config.example.json
   /bin/cp -fv rabbitmq_exporter_config.json.cnf /opt/app/rabbitmq_exporter-1.0.0/config.json

   ln -s /opt/app/mysqld_exporter-0.15.1.linux-amd64 /opt/app/mysqld_exporter
   /bin/cp -fv system/mysqld_exporter.service /usr/lib/systemd/system/mysqld_exporter.service
}

download() {
   cd /opt/soft
   download_prometheus
   download_alertmanager
   download_grafana
   #  download_pushgateway
   #  download_thanos
   download_goreman
   download_exporter
}

start_standalone() {
   systemctl daemon-reload
   systemctl start prometheus.service
   systemctl start alertmanager.service
   systemctl start grafana.service
   # journalctl -u prometheus.service
   # journalctl -u alertmanager.service
   # systemctl stop prometheus.service
   # systemctl stop alertmanager.service
}

start_prometheus_culster() {
   cd /opt/app
   cat >p.procfile <<EOF
p1: prometheus --web.enable-lifecycle --web.listen-address="0.0.0.0:9090" --storage.tsdb.min-block-duration=2h --storage.tsdb.max-block-duration=2h --storage.tsdb.retention.time=4h --storage.tsdb.path=/opt/app/prometheus-2.52.0.linux-amd64/data9090 --config.file=/opt/app/prometheus-2.52.0.linux-amd64/prometheus.yml --log.level=info

t1: thanos sidecar --prometheus.url="http://127.0.0.1:9090" --http-address=0.0.0.0:19191 --grpc-address=0.0.0.0:19091 --tsdb.path=/opt/app/prometheus-2.52.0.linux-amd64/data --objstore.config-file=/opt/app/thanos-0.35.1.linux-amd64/thanos_sidecar.yml --log.level=info

ts1: thanos store --data-dir=/tmp/thanos-store --objstore.config-file=/opt/app/thanos-0.35.1.linux-amd64/thanos_sidecar.yml --http-address=0.0.0.0:19192 --grpc-address=0.0.0.0:19092

p2: prometheus --web.enable-lifecycle --web.listen-address="0.0.0.0:9091" --storage.tsdb.min-block-duration=2h --storage.tsdb.max-block-duration=2h --storage.tsdb.retention.time=4h --storage.tsdb.path=/opt/app/prometheus-2.52.0.linux-amd64/data9091 --config.file=/opt/app/prometheus-2.52.0.linux-amd64/prometheus1.yml --log.level=info

t2: thanos sidecar --prometheus.url="http://127.0.0.1:9091" --http-address=0.0.0.0:29191 --grpc-address=0.0.0.0:29091 --tsdb.path=/opt/app/prometheus-2.52.0.linux-amd64/data1 --objstore.config-file=/opt/app/thanos-0.35.1.linux-amd64/thanos_sidecar.yml --log.level=info

ts2: thanos store --data-dir=/tmp/thanos-store2 --objstore.config-file=/opt/app/thanos-0.35.1.linux-amd64/thanos_sidecar.yml --http-address=0.0.0.0:29192 --grpc-address=0.0.0.0:29092

tq1: thanos query --http-address=0.0.0.0:19193 --grpc-address=0.0.0.0:19093 --store=127.0.0.1:19091 --store=127.0.0.1:29091 --store=127.0.0.1:19092 --store=127.0.0.1:29092
EOF

   # 启动进程
   # goreman -f p.procfile start p1 p2 t1 t2 ts1 ts2 tq1
}

start_alertmanager_cluster() {
   cd /opt/app
   cat >a.procfile <<EOF
a1: alertmanager --web.listen-address=":9093" --cluster.listen-address="127.0.0.1:8001" --config.file=/opt/app/alertmanager-0.27.0.linux-amd64/alertmanager.yml --storage.path=/opt/app/alertmanager-0.27.0.linux-amd64/data9093 --log.level=debug

a2: alertmanager --web.listen-address=":9094" --cluster.listen-address="127.0.0.1:8002" --cluster.peer=127.0.0.1:8001 --config.file=/opt/app/alertmanager-0.27.0.linux-amd64/alertmanager.yml --storage.path=/opt/app/alertmanager-0.27.0.linux-amd64/data9094 --log.level=debug

a3: alertmanager --web.listen-address=":9095" --cluster.listen-address="127.0.0.1:8003" --cluster.peer=127.0.0.1:8001 --config.file=/opt/prometheus/app/alertmanager-0.27.0.linux-amd64/alertmanager.yml  --storage.path=--storage.path=/opt/app/alertmanager-0.27.0.linux-amd64/data9095 --log.level=debug

w1: webhook
EOF

   # goreman -f a.procfile start a1 a2 a3 w1
}

start() {
   # curl localhost:3000
   systemctl start grafana-server.service

   cd /opt/app


   cat >Procfile <<EOF
# goreman start p1 a1 w1

# curl localhost:9090
p1: prometheus --web.enable-lifecycle --web.listen-address="0.0.0.0:9090" --storage.tsdb.min-block-duration=2h --storage.tsdb.max-block-duration=2h --storage.tsdb.retention=4h --storage.tsdb.path=/opt/app/prometheus-2.52.0.linux-amd64/data9090 --config.file=/opt/app/prometheus-2.52.0.linux-amd64/prometheus.yml --log.level=info --web.external-url=http://prome.com:9090

# curl localhost:9093
a1: alertmanager --web.listen-address=":9093" --cluster.listen-address="127.0.0.1:8001" --config.file=/opt/app/alertmanager-0.27.0.linux-amd64/alertmanager.yml --storage.path=/opt/app/alertmanager-0.27.0.linux-amd64/data9093 --log.level=debug --web.external-url=http://alert.com:9093

w1: webhook

# curl 127.0.0.1:9100/metrics
n1: node_exporter --web.listen-address=:9100 --web.max-requests=40 --log.level=info
n2: node_exporter --web.listen-address=:9101 --web.max-requests=40 --log.level=info
n3: node_exporter --web.listen-address=:9102 --web.max-requests=40 --log.level=info

# curl 127.0.0.1:9115/metrics
# curl 'http://127.0.0.1:9115/probe?module=http_2xx&target=baidu.com'
b1: blackbox_exporter --web.listen-address=:9115 --config.file="/opt/app/blackbox_exporter-0.25.0.linux-amd64/blackbox.yml" --log.level=info

# curl 127.0.0.1:9121/metrics
r1: redis_exporter --web.listen-address=:9121 -redis.addr 10.243.0.196:6000 -redis.password N2IwOWUzMWE2MmMy

# curl 127.0.0.1:9104/metrics
m1: mysqld_exporter --web.listen-address=:9104 --web.telemetry-path="/metrics" --config.my-cnf="/opt/app/mysqld_exporter-0.15.1.linux-amd64/my.cnf" --log.level=debug

# curl 127.0.0.1:9308/metrics
k1: kafka_exporter --web.listen-address=":9308" --kafka.server=10.223.12.26:9092

# curl 127.0.0.1:9216/metrics
mg1: mongodb_exporter --web.listen-address=":9216" --mongodb.uri=mongodb://admin:123456@10.243.0.39:27017/scc_order --log.level=info --compatible-mode --collector.replicasetstatus --collector.topmetrics --collector.diagnosticdata --collector.dbstats

# curl 127.0.0.1:9419/metrics
rq1: rabbitmq_exporter --config-file /opt/app/rabbitmq_exporter-1.0.0/config.json
EOF

   # goreman start p1 a1 n1
}

system() {
   cat >/usr/lib/systemd/system/mysqld-exporter.service <<EOF
[Unit]
Description=mysqld-exporter
After=network.target

[Service]
Type=simple
ExecStart=/opt/app/mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter --web.listen-address=:9104 --web.telemetry-path="/metrics" --config.my-cnf=/opt/app/mysqld_exporter-0.15.1.linux-amd64/my.cnf --log.level=info
Restart=always

[Install]
WantedBy=multi-user.target
EOF
}

case "${1}" in
download)
   # download -- config -- run
   # prometheus -- alertmanger -- grafana -- exporter/metricsAPI
   download
   ;;
start)
   # start_standalone
   # start_prometheus_culster
   # start_alertmanager_cluster

   # goreman 管理多进程，debug
   start
   ;;
*)
   echo "download|start"
   ;;
esac
